package com.pureut.storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.storage.domain.PublicMaterial;
import com.pureut.storage.mapper.PublicMaterialMapper;
import com.pureut.storage.service.PublicGoodsService;
import com.pureut.storage.vo.MaterialMqVo;
import com.sale.common.redis.utils.RedisUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/***
 * 公共物料实现层
 * Author:C
 * Date:2023/03/22 11:01
 * @DESC
 */
@Service
public class PublicGoodsServiceImpl implements PublicGoodsService {

    @Resource
    PublicMaterialMapper publicMaterialMapper;

    /**
     * 新增
     *
     * @param mq
     */
    @Override
    public void insertPublicGoods(MaterialMqVo mq) {
        PublicMaterial publicMaterial1 = publicMaterialMapper.selectOne(new QueryWrapper<PublicMaterial>().lambda().eq(PublicMaterial::getMaterialCode, mq.getMaterialCode()));
        if(publicMaterial1 == null){
            PublicMaterial publicMaterial = new PublicMaterial();
            BeanUtils.copyProperties(mq,publicMaterial);
            //获取用户id
            String tenantId = RedisUtils.getCacheObject(mq.getUserIdStr());
            publicMaterial.setTenantId(Long.parseLong(tenantId));
            publicMaterialMapper.insert(publicMaterial);
        }
    }

    /**
     * 修改
     *
     * @param mq
     */
    @Override
    public void updatePublicGoods(MaterialMqVo mq) {
        //根据物料编码获取数据
        publicMaterialMapper.updateData(mq);

    }

    /**
     * 删除
     * @param mq
     */
    @Override
    public void deletePublicGoods(MaterialMqVo mq) {
        List<String> idList = Arrays.asList(mq.getIdStr().split(","));
        publicMaterialMapper.deleteData(idList);
    }
}
